package com.cincc.cinccimdemo.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.Constants;
import com.cincc.cinccimdemo.common.DownloadUtil;
import com.cincc.cinccimdemo.common.FileUtils;
import com.cincc.cinccimdemo.common.GlideUtils;
import com.cincc.cinccimdemo.common.MyUtils;
import com.cincc.cinccimdemo.model.MessageInfoUI;
import com.cincc.cinccimdemo.viewholder.PreviewImageHolder;
import com.cincc.cinccimdemo.viewholder.PreviewVideoHolder;
import com.cincc.im.callback.ImCallback;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.model.ImResponse;
import com.cincc.im.model.enums.ImSendType;
import com.cincc.im.model.enums.MessageType;
import com.cincc.im.model.message.ImImageMessage;
import com.cincc.im.model.message.ImVideoMessage;

import java.util.List;

import cn.jzvd.JzvdStd;

public class PreviewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "PreviewAdapter";
    public static final int TYPE_IMAGE = 0;
    public static final int TYPE_VIDEO = 1;
    List<MessageInfoUI> list;
    Activity context;
    int currentIndex;

    public PreviewAdapter(Activity context, List<MessageInfoUI> list, int current) {
        this.context = context;
        this.list = list;
        this.currentIndex = current;
    }

    @Override
    public int getItemViewType(int position) {
        MessageInfoUI mediaEntity = list.get(position);
        if (mediaEntity.getMessageInfo().messageType == MessageType.IMAGE)
            return TYPE_IMAGE;
        else return TYPE_VIDEO;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_IMAGE) {
            View view = LayoutInflater.from(context).inflate(R.layout.preview_photo, parent, false);
            return new PreviewImageHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.preview_video, parent, false);
            return new PreviewVideoHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MessageInfoUI item = list.get(position);
        if (holder.getItemViewType() == TYPE_IMAGE) {
            PreviewImageHolder imageHolder = (PreviewImageHolder) holder;
            ImImageMessage imImageMessage = (ImImageMessage) item.getMessageInfo().imMessage;
            GlideUtils.initImageNoCache(context, imImageMessage.msgContent, imageHolder.photoView);
            imageHolder.photoView.setOnClickListener(v -> {
                onItemClickListener.itemClick();
            });
        } else {
            ImVideoMessage imMessage = (ImVideoMessage) item.getMessageInfo().imMessage;
            PreviewVideoHolder videoHolder = (PreviewVideoHolder) holder;
            GlideUtils.initImageNoCache(context, imMessage.thumb, videoHolder.photoView);
            videoHolder.btnImageView.setOnClickListener(v -> {
                videoHolder.photoView.setVisibility(View.GONE);
                videoHolder.btnImageView.setVisibility(View.GONE);
                checkVideo(videoHolder, item);
            });
            videoHolder.photoView.setOnClickListener(v -> {
                onItemClickListener.itemClick();
            });
//            if (position == currentIndex) {
//                videoHolder.photoView.setVisibility(View.GONE);
//                videoHolder.btnImageView.setVisibility(View.GONE);
//                checkVideo(videoHolder, item);
//            }
        }
    }

    private void checkVideo(PreviewVideoHolder videoHolder, MessageInfoUI item) {
        ImVideoMessage imMessage = (ImVideoMessage) item.getMessageInfo().imMessage;
        String videoUrl = imMessage.msgContent;
        if (TextUtils.isEmpty(item.getMessageInfo().localPath)) {
            videoHolder.progressBar.setVisibility(View.VISIBLE);
            IMSipPhoneCtrl.getInstance().downloadImFile(item.getRemoteMsgId(),
                    item.getMessageInfo().messageType.value(), ImSendType.isP2P(item.getMessageInfo().imSendType) ? item.getMessageInfo().senderId : item.getMessageInfo().groupId,
                    item.getMessageInfo().imSendType, videoUrl, imResponse -> {
                        if (imResponse.getCode() == 0) {
                            String path = (String) imResponse.getData();
                            imMessage.msgContent = path;
                            onItemUpdateListener.itemUpdate(item);
                            playVideo(videoHolder, path);
                        }
                        videoHolder.progressBar.setVisibility(View.GONE);
                    });
        } else {
            playVideo(videoHolder, item.getMessageInfo().localPath);
        }
    }

    private void playVideo(PreviewVideoHolder videoHolder, String videoPath) {
        context.runOnUiThread(() -> {
            previewVideoHolder = videoHolder;
            videoHolder.videoView.setVisibility(View.VISIBLE);
            videoHolder.videoView.setUp(videoPath, "");
            videoHolder.videoView.setOnCompleteListener(() -> {
                videoHolder.videoView.setVisibility(View.GONE);
                videoHolder.photoView.setVisibility(View.VISIBLE);
                videoHolder.btnImageView.setVisibility(View.VISIBLE);
            });
            videoHolder.videoView.startVideo();
        });
    }

    PreviewVideoHolder previewVideoHolder;

    public void resetView() {
        if (previewVideoHolder != null) {
            JzvdStd.releaseAllVideos();
            previewVideoHolder.videoView.setVisibility(View.GONE);
            previewVideoHolder.photoView.setVisibility(View.VISIBLE);
            previewVideoHolder.btnImageView.setVisibility(View.VISIBLE);
            previewVideoHolder = null;
        }
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface OnItemUpdateListener {
        void itemUpdate(MessageInfoUI message);
    }

    private OnItemUpdateListener onItemUpdateListener;

    public void setOnItemUpdateListener(OnItemUpdateListener onItemClickListener) {
        this.onItemUpdateListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void itemClick();
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
